﻿/*
	Class Description: Panel GUI built using graphics API
	------------------------------------------------------------------------
	                          APACHE LICENSE
	------------------------------------------------------------------------
	Copyright 2010 Vu Thien Nguyen Phong
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package com.minidebugger.gui {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.MouseEvent;

	public class Panel extends Sprite{
		private var title:String;
		private var w:int;
		private var h:int;
		private var boundRadius:Number;
		private var thickness:int;
		private var borderColor:uint;
		private var bgColor:uint;
		
		private var title_txt:TextField;
		private var draggingMask:Sprite;
		
		public function Panel(title:String,w:int,h:int,boundRadius:Number=10,thickness:int=1,borderColor:uint = 0xffaaaaaa,bgColor:uint=0xfffffffe) {
			this.title = title
			this.w = w;
			this.h = h;
			this.boundRadius = boundRadius;
			this.thickness = thickness;
			this.borderColor = borderColor;
			this.bgColor = bgColor;
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		private function init(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			// entry point
			draw();
		}
		
		private function draw():void{
			this.graphics.lineStyle(thickness,borderColor);
			this.graphics.beginFill(bgColor);
			this.graphics.drawRoundRect(0,0,w,h,boundRadius,boundRadius);
			this.graphics.endFill();
			
			addTitle();
			addDraggingMask();
		}
		
		// adding the panel title
		private function addTitle():void{
			var title_tf:TextFormat = new TextFormat();
			title_tf.font = "Arial";
			title_tf.bold = true;
			title_tf.size = 11;
			
			title_txt = new TextField();
			title_txt.autoSize = "left";
			title_txt.x = 5;
			title_txt.y = 5;
			title_txt.mouseEnabled = false;
			title_txt.defaultTextFormat = title_tf;
			title_txt.text = title;
			
			this.addChild(title_txt);
		}
		
		// add a transparent sprite on top of the panel - this is the
		// only area that users can hold to drag and drop panel
		private function addDraggingMask():void{
			draggingMask = new Sprite();
			draggingMask.buttonMode = true;
			draggingMask.graphics.beginFill(0xff000000);
			draggingMask.graphics.drawRect(0,0,w,20)
			draggingMask.graphics.endFill();
			draggingMask.addEventListener(MouseEvent.MOUSE_DOWN,startDragPanel);
			draggingMask.addEventListener(MouseEvent.MOUSE_UP,stopDragPanel);
			draggingMask.alpha = 0;
			this.addChild(draggingMask);
		}
		
		private function startDragPanel(e:Event):void{
			this.startDrag();
		}
		
		private function stopDragPanel(e:Event):void{
			this.stopDrag();
		}
	}
}
